# -*- coding: utf-8 -*-
from .base import BaseTool
from .base import PrintUtils,CmdTask,FileUtils,AptUtils,ChooseTask
from .base import osversion,osarch
from .base import run_tool_file
import re


class Tool(BaseTool):
    def __init__(self):
        self.type = BaseTool.TYPE_CONFIG
        self.name = "一键配置Qt交叉编译环境"
        self.autor = 'hyw666'

    def install_pc_qt_creator(self):
        pattern = r'^\d+(\.\d+){2}$'
        version = {
            1: {'tip':'debian'},
            2: {'tip':'ubuntu'},
        }
        #交叉编译器
        choose_dic={}
        for tool_id in version.keys(): choose_dic[tool_id]  = version[tool_id]["tip"]
        code1,result = ChooseTask(choose_dic, "需要确认你板卡的系统:").run()
        #编译源码
        dic = {1:"5.15.8",2:"其他版本"}
        code2,result = ChooseTask(dic, "需要编译qt源码版本是?").run()

        if  code2==2:
            version ={}
            qt_version=input("请输入需要编译的qt源码版本: ")
            if re.match(pattern, qt_version):
                PrintUtils.print_info('输入正确')
                code3=1
            else:
                code3=0
                PrintUtils.print_info('输入错误')

        if  code1==1:
            CmdTask("sudo tools/qt/build-gcc-aarch64-linux-gnu-8.3.0.sh", 0).run()
        else:
            CmdTask("sudo apt update", 0).run()
            CmdTask("sudo apt install -y gcc-aarch64-linux-gnu", 0).run()

        if  code2==1:
            CmdTask("sudo tools/qt/build-qt-5.15.8.sh", 0).run()

        if  code3==1:
            CmdTask("sudo tools/qt/build-qt-user-defined.sh {}".format(qt_version), 0).run()




    def run(self):
        if osarch=='amd64':
            #正式的运行
            self.install_pc_qt_creator()
        else:
            PrintUtils.print_info('不是amd64环境,板卡在安装Qt-Creator已经搭建Qt编译环境')  
